import Tkinter
import Feeds

class record():
    window= None
    reader = None
    curFeed = None
    frames = None
    checks = None
    maxRow = None

def run(newsReader):
    record.window = Tkinter.Toplevel()
    record.reader = newsReader
    record.curFeed = 'main'
    
    record.maxRow = max(len(newsReader.feeds),
                 len(newsReader.ribbons['opinion'].feeds),
                 len(newsReader.ribbons['ref'].feeds))
    F = Tkinter.Frame(record.window,
                      width=304, height=22*(record.maxRow+1)+4,
                      bd=2, relief='groove')
    F.grid_propagate(0)
    F.pack(side='top')
    f = {}
    f['main'] = Tkinter.Frame(F,
                              width=100, height=22,
                              bd=2, relief='flat')
    f['main'].pack_propagate(0)
    f['main'].grid(row=0,column=0)
    f['opinion'] = Tkinter.Frame(F,
                              width=100, height=22,
                              bd=2, relief='groove')
    f['opinion'].pack_propagate(0)
    f['opinion'].grid(row=0,column=1)
    f['ref'] = Tkinter.Frame(F,
                              width=100, height=22,
                              bd=2, relief='groove')
    f['ref'].pack_propagate(0)
    f['ref'].grid(row=0,column=2)
    f['feeds'] = Tkinter.Frame(F,
                               width=300, height=22*record.maxRow)
    f['feeds'].pack_propagate(0)
    f['feeds'].grid(row=1,column=0,columnspan=3)
    record.frames = f
    
    Tkinter.Button(f['main'],
                   text='Main',
                   bd=0,relief='flat',
                   command=lambda: _setFeeds('main'))\
                   .pack(fill='both',expand=1)
    Tkinter.Button(f['opinion'],
                   text='Op-Ed',
                   bd=0,relief='flat',
                   command=lambda: _setFeeds('opinion'))\
                   .pack(fill='both',expand=1)
    Tkinter.Button(f['ref'],
                   text='Ref',
                   bd=0,relief='flat',
                   command=lambda: _setFeeds('ref'))\
                   .pack(fill='both',expand=1)
    Tkinter.Button(record.window,
                   text='Add Source to Feed',
                   command=lambda: _ask())\
                   .pack(side='left')
    Tkinter.Button(record.window,
                   text='Cancel',
                   command=lambda: record.window.destroy())\
                   .pack(side='right')
    Tkinter.Button(record.window,
                   text='Delete Selected Sources',
                   command=lambda: _delFeeds())\
                   .pack(side='top')

    _setFeeds('main')
    record.window.mainloop()

def _setFeeds(option):
    record.checks = []
    record.curFeed = option
    
    record.frames['main'].__setitem__('relief','groove')
    record.frames['opinion'].__setitem__('relief','groove')
    record.frames['ref'].__setitem__('relief','groove')
    record.frames[option].__setitem__('relief','flat')
    
    F = record.frames['feeds'].master
    record.frames['feeds'].grid_remove()
    record.frames['feeds'] = Tkinter.Frame(F,
                                      width=300, height=22*record.maxRow)
    record.frames['feeds'].pack_propagate(0)
    record.frames['feeds'].grid(row=1,column=0,columnspan=3)
    for i in range(len(record.reader.ribbons[option].feeds)):
        record.checks += [Tkinter.IntVar()]
        Tkinter.Checkbutton(record.frames['feeds'],
                            text=record.reader.ribbons[option].feeds[i].sourceName,
                            variable=record.checks[i],
                            onvalue=1,
                            offvalue=0)\
                            .pack(side='top')

def _delFeeds():
    for i in range(len(record.checks),0,-1):
        if record.checks[i-1].get()==1:
            record.reader.ribbons[record.curFeed].feeds.remove(record.reader.ribbons[record.curFeed].feeds[i-1])
    record.window.destroy()
    for x in record.reader.ribbons.values():
        x.build()

def _ask():
    t = Tkinter.Toplevel()
    e = Tkinter.Entry(t,
                      width=50,
                      text='new URL:')
    e.pack()
    Tkinter.Button(t,
                   text='Add Feed',
                   command = lambda : _addFeed(t,e.get()))\
                   .pack()

def _addFeed(top, feedURL):
    if _checkURL(feedURL):
        record.reader.feeds += [Feeds.URLfeed(feedURL)]
        Feeds.create(record.reader.feeds)
        for x in record.reader.ribbons.values():
            x.build()
        top.destroy()

def _checkURL(feedURL):
    return True
